home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / ctldlt.zip / CTLCOL.CPP < prev    next >
C/C++ Source or Header  |  1992-09-09  |  20KB  |  417 lines

  1. #include <owl.h>         // CTLCOL adds a Colored Rectangle Custom
  2. #include <bwcc.h>        // Control that interfaces with the
  3. #include <custcntl.h>    // The Resource Workshop as well as
  4. #include <control.h>     // functions as a stand alone control.
  5. #include <static.h>      // It Parses ~ delimited data added to
  6. #include <edit.h>        // the title field to define the Rectangle's
  7. #include <bstatic.h>     // color.  Also adds the color Common dialog
  8. #include <commdlg.h>     // to allow a color selection from the
  9. #include "ctlcol.h"      // resource workshop.
  10.  
  11. BOOL fInWorkshop;
  12. PTModule CtlColModule;
  13. //==================================================================
  14. _CLASSDEF(TColorDialog)               // Class to encapsulate
  15. class TColorDialog : public TDialog   // the Windows 3.1
  16.   {                                   // Color Common Dialog
  17.   public:
  18.     DWORD dwCustClrs[16];             // Array for custom colors
  19.     TColorDialog(PTWindowsObject AParent, // Constructor
  20.          DWORD * dwNewColor, LPSTR lpName, PTModule AModule);
  21.     virtual BOOL Create();            // Create for non-modal Creation
  22.     virtual int Execute();            // Execute for modal Execution
  23.     virtual void CloseWindow(int ARetValue){}; // trap destruction
  24.   protected:
  25.     CHOOSECOLOR ccdlg;                // Color Common Dialog Structure
  26.     DWORD *dwColor;                   // Color Variable
  27.   };
  28. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
  29. TColorDialog::TColorDialog(PTWindowsObject AParent, DWORD * dwNewColor,
  30.                    LPSTR AName,PTModule AModule = NULL)
  31.          :TDialog(AParent,AName,AModule) // Constructor calls base
  32.   {
  33.   dwColor =  dwNewColor;        //equate color address to allow updating
  34.   int Count;                    //initialize the 16 custom colors to
  35.   for (Count = 0; Count <= 15; Count++) // Shades of blue
  36.     dwCustClrs[Count] = RGB(Count*(255/15),Count*(255/15),255);
  37.   ccdlg.lStructSize    = sizeof(CHOOSECOLOR);    // initialize ccdl struct
  38.   ccdlg.hwndOwner      = GetFocus();             // parent has the focus
  39.   ccdlg.hInstance      = NULL;                   // Not Used for default dlg
  40.   ccdlg.rgbResult      =  *dwNewColor;           // Color in
  41.   ccdlg.lpCustColors   = (LPDWORD)(dwCustClrs);  // array of blue colors
  42.   ccdlg.Flags          = CC_FULLOPEN|CC_RGBINIT; // full dialog with color in
  43.   ccdlg.lCustData      = 0L;                     // Use Default Data
  44.   ccdlg.lpfnHook       = (FARPROC)NULL;          // No Message trapping
  45.   ccdlg.lpTemplateName = (LPSTR)NULL;            // Use Default dialog
  46.   };
  47. BOOL TColorDialog::Create()                      // Called by MakeWindow
  48.   {
  49.   ccdlg.hwndOwner = NULL;                        // No parent for non-Model
  50.   ChooseColor(&ccdlg);                           // ccdlg function call
  51.   *dwColor = ccdlg.rgbResult;                    // Color out
  52.   return 1;                                      // Success
  53.   }
  54. int TColorDialog::Execute()                      // Called by CreateDialog
  55.   {
  56.   ChooseColor(&ccdlg);                           // ccdlg function call
  57.   *dwColor = ccdlg.rgbResult;                    // Color out
  58.   return 1;                                      // Success
  59.   }
  60. //=====================================================================
  61. //                   TColRect Custom Control
  62. char szClassName[]  = "ColRect";   // class for new custom control
  63. //=====================================================================
  64. #pragma argsused  // TColRect methods prototype in header file
  65. TColRect::TColRect(PTWindowsObject AParent, int AnId,
  66.              int X, int Y, int W, int H,
  67.              LPSTR ATitle,
  68.              WORD  ATextLen,
  69.              PTModule AModule)
  70.             :TStatic (AParent, AnId, ATitle,X,Y,W,H,
  71.                   ATextLen,AModule){};
  72. //------------------------------------------------------------------------
  73. TColRect::TColRect(PTWindowsObject AParent, int ResourceId,
  74.                PTModule AModule)
  75.               :TStatic(AParent, ResourceId, 1, AModule){};
  76. //---------------------------------------------------------------------
  77. // PARSE function used by SetupWindow to pull 4 text fields out of one
  78. //       Title field delimited by the tilde character ~
  79. int TColRect::Parse(int StartChar)
  80.   {
  81.   int Count = 0;
  82.   while ((szTitle[StartChar] != '~')&(szTitle[StartChar] != NULL))
  83.     {
  84.     szBuffer[Count] = szTitle[StartChar];
  85.     StartChar++;
  86.     Count++;
  87.     };
  88.   szBuffer[Count] = NULL;
  89.   return StartChar;
  90.   }
  91. //---------------------------------------------------------------------
  92. void TColRect::SetupWindow()
  93.   {
  94.   int nCharCount = 0;
  95.   TStatic::SetupWindow();               // call base class
  96.   GetText(szTitle,254);                 // Get Title
  97.   nCharCount   = Parse(nCharCount);     // Parse out title
  98.   nCharCount   = Parse(nCharCount+1);   // Parse out Red Value
  99.   int nRed     = atoi(szBuffer);
  100.   nCharCount   = Parse(nCharCount+1);   // Parse out Green
  101.   int nGreen   = atoi(szBuffer);
  102.   nCharCount   = Parse(nCharCount+1);   // Parse out Blue
  103.   int nBlue    = atoi(szBuffer);
  104.   DisplayColor = RGB(nRed,nGreen,nBlue); // Combine into a Color
  105.   if (fInWorkshop)                               // If in Workshop
  106.      InvalidateRect(Parent->HWindow,NULL,FALSE); // Update Parent
  107.   }
  108. //------------------------------------------------------------------------
  109. #pragma argsused                   // Public interface function
  110. void TColRect::WMColor(RTMessage Msg)// for TColRect Custom control
  111.   {                                // for prototypes in header file.
  112.                    // Note the use of function pairs
  113.   DisplayColor = Msg.LParam;
  114.   InvalidateRect(HWindow,NULL,FALSE);
  115.   return;                          // to send a message from a member
  116.   }                                // function through windows to allow
  117. void TColRect::Color(COLORREF NewColor)// the dummy object to access data
  118.   {                                // in the display object.
  119.   SendMessage(HWindow,CR_COLOR,1,NewColor);
  120.   }
  121. //------------------------------------------------------------------------
  122. void TColRect::WMPaint(RTMessage Msg)
  123.   {
  124.   HPEN   hPen,   hOldPen;                       // Pen Variables
  125.   HBRUSH hBrush, hOldBrush;                     // Brush Variables
  126.   HDC hDC = GetDC(HWindow);                     // grab the Device Context
  127.   hPen      = CreatePen(PS_SOLID,1,DisplayColor); // Colored pen
  128.   hOldPen   = (HPEN)SelectObject(hDC,hPen);     // use  pen
  129.   hBrush    = (HBRUSH)CreateSolidBrush(DisplayColor); // Colored brush
  130.   hOldBrush = (HBRUSH)SelectObject(hDC,hBrush); // use brush
  131.   Rectangle(hDC,0,0,Attr.W,Attr.H);             // output Colored rectangle
  132.   SelectObject(hDC,hOldBrush);                  // restore old brush
  133.   DeleteObject(hBrush);                         // destroy new brush
  134.   SelectObject(hDC,hOldPen);                    // restore pen
  135.   DeleteObject(hPen);                           // destroy new pen
  136.   ReleaseDC(HWindow,hDC);                       // release the Device Context
  137.   ValidateRect(HWindow,NULL);                   // Mark the window as painted
  138.   Msg.Result = 0;                               // Success
  139.   }
  140. //------------------------------------------------------------------------
  141. LPSTR TColRect::GetClassName()
  142.       {
  143.       return szClassName;
  144.       }
  145. //------------------------------------------------------------------------
  146. LONG FAR PASCAL _export ColRectWndProc(HWND HWindow,  // Main process
  147.                     WORD wMsg,    // registered
  148.                     WORD wParam,  // in following
  149.                     LONG lParam)  // function
  150.   {
  151.   if (wMsg == WM_CREATE)    // if Called by non-OWL application (RW)
  152.     {
  153.     PTColRect PColRect =  // Create an instance
  154.       new TColRect(CtlColModule->GetParentObject(GetParent(HWindow)),
  155.              GetWindowWord(HWindow, GWW_ID),
  156.              CtlColModule);
  157.     CtlColModule->MakeWindow(PColRect); // hook module to a window
  158.     }
  159.   return DefWindowProc(HWindow, wMsg, wParam, lParam); // allow Windows
  160.   }          // to handle messages prior to OWL hooking into the window
  161. //------------------------------------------------------------------------
  162. BOOL RegisterColRectClass(HINSTANCE hInstance)  // Called by Libmain to
  163. {                                           // register the Window Class
  164.   WNDCLASS  wc;
  165.   wc.style         = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS | CS_GLOBALCLASS;
  166.   wc.lpfnWndProc   = (WNDPROC)ColRectWndProc;  // Registers Proc used by Class
  167.   wc.cbClsExtra    = 0;
  168.   wc.cbWndExtra    = 0;
  169.   wc.hInstance     = hInstance;
  170.   wc.hIcon         = NULL;
  171.   wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  172.   wc.hbrBackground = (HBRUSH)(NULL_BRUSH);    //(Clear Background);
  173.   wc.lpszMenuName  = NULL;
  174.   wc.lpszClassName   = szClassName;
  175.   return RegisterClass(&wc);
  176.   }
  177. //------------------------------------------------------------------------
  178. _CLASSDEF(TColRectDlg)              // Workshop Functions
  179. class TColRectDlg : public TDialog  // Dialog Class used by Workshop when
  180.   {                               // The Control is double clicked
  181.   protected:
  182.     LPFNSTRTOID    StrToId;       // Pointers to items in workshop
  183.     LPFNIDTOSTR    IdToStr;       // That are used in dialog
  184.     LPCTLSTYLE     Style;
  185.     PTEdit         pTitle;        // Control pointers
  186.     PTEdit         pIdText;
  187.     PTBStatic      pIdNum;
  188.     PTEdit         pRed;
  189.     PTEdit         pGreen;
  190.     PTEdit         pBlue;
  191.     char           szBuffer[80];  // Line Buffer
  192.     WORD           wType;         // Control Type
  193.     public:
  194.     TColRectDlg(PTWindowsObject AParent, LPSTR AName,
  195.           LPFNSTRTOID PStrToId, LPFNIDTOSTR PIdToStr,
  196.           LPCTLSTYLE  PStyle, PTModule AModule = NULL);
  197.     int Parse(int StartChar);
  198.     void SetupWindow();
  199.     virtual void PaletteButton(RTMessage Msg) // button handler
  200.       = [ID_FIRST + IDB_PALETTE];
  201.     void Ok(RTMessage Msg) = [ID_FIRST + IDOK];
  202.   };
  203. //------------------------------------------------------------------------
  204. TColRectDlg::TColRectDlg(PTWindowsObject AParent, LPSTR AName,
  205.              LPFNSTRTOID PStrToId, LPFNIDTOSTR PIdToStr,
  206.              LPCTLSTYLE  PStyle, PTModule AModule)
  207.       :TDialog(AParent, AName, AModule),    // Base Class
  208.        StrToId(PStrToId),                   // Three functions
  209.        IdToStr(PIdToStr),                   // in Worshop that
  210.        Style(PStyle)                        // are used
  211.   {
  212.   pTitle      = new TEdit(this,      IDD_TITLE, CTLTITLE, CtlColModule);
  213.   pIdText     = new TEdit(this,      IDD_IDTEXT, 255, CtlColModule);
  214.   pIdNum      = new TBStatic(this,   IDD_IDNUM,   18, CtlColModule);
  215.   pRed        = new TEdit(this,      IDD_IDRED,    4, CtlColModule);
  216.   pGreen      = new TEdit(this,      IDD_IDGREEN,  4, CtlColModule);
  217.   pBlue       = new TEdit(this,      IDD_IDBLUE,   4, CtlColModule);
  218.   }
  219. //---------------------------------------------------------------------
  220. // PARSE function used by SetupWindow to pull 4 text fields out of one
  221. //       Title field delimited by the tilde character ~
  222. int TColRectDlg::Parse(int StartChar)
  223.   {
  224.   int Count = 0;
  225.   while ((Style->szTitle[StartChar] != '~')&(Style->szTitle[StartChar] != NULL))
  226.     {
  227.     szBuffer[Count] = Style->szTitle[StartChar];
  228.     StartChar++;
  229.     Count++;
  230.     };
  231.   szBuffer[Count] = NULL;
  232.   return StartChar;
  233.   }
  234. //---------------------------------------------------------------------
  235. void TColRectDlg::SetupWindow()
  236.   {
  237.   int   nCharCount = 0;
  238.   char  buf[256];
  239.   TDialog::SetupWindow();             // call base class
  240.   nCharCount = Parse(nCharCount);     // Parse 4 fields out of title
  241.   pTitle->SetText(szBuffer);          // and initialize controls
  242.   nCharCount = Parse(nCharCount+1);
  243.   pRed->SetText(szBuffer);
  244.   nCharCount = Parse(nCharCount+1);
  245.   pGreen->SetText(szBuffer);
  246.   nCharCount = Parse(nCharCount+1);
  247.   pBlue->SetText(szBuffer);
  248.  
  249.   IdToStr(Style->wId, buf, 255);      // Use Workshop to get ID
  250.   pIdText->SetText(buf);              // String and store it
  251.   itoa(Style->wId, buf, 10);          // Translate it to a Number
  252.   pIdNum->SetText(buf);               // and Store Number
  253.   wType = 0xFFFFL & LOWORD(Style->dwStyle); // Save control Type
  254.   }
  255. //----------------------------------------------------------------------
  256. void TColRectDlg:: PaletteButton(RTMessage)     // button activates
  257.   {
  258.   char szColorBuf[6];
  259.   pRed->GetText(szColorBuf, 4);          // Get Red Mix
  260.   int nRed  = atoi(szColorBuf);
  261.   pGreen->GetText(szColorBuf, 4);        // Get Green Mix
  262.   int nGreen  = atoi(szColorBuf);
  263.   pBlue->GetText(szColorBuf, 4);         // Get Blue Mix
  264.   int nBlue  = atoi(szColorBuf);
  265.   DWORD dwNewColor = RGB(nRed,nGreen,nBlue); // Combine into a Color
  266.  
  267.   CtlColModule->ExecDialog(new TColorDialog(
  268.                 CtlColModule->GetParentObject(HWindow),
  269.                 &dwNewColor,"Color_Dialog",CtlColModule));
  270.  
  271.   wsprintf (szColorBuf,"%d",GetRValue(dwNewColor));
  272.   pRed->SetText(szColorBuf);
  273.   wsprintf (szColorBuf,"%d",GetGValue(dwNewColor));
  274.   pGreen->SetText(szColorBuf);
  275.   wsprintf (szColorBuf,"%d",GetBValue(dwNewColor));
  276.   pBlue->SetText(szColorBuf);
  277.   }
  278. //----------------------------------------------------------------------
  279. #pragma argsused
  280. void TColRectDlg::Ok(RTMessage Msg)      // OK Button pressed
  281.   {
  282.   char  szBuf[256];                   // Buffer for ID string
  283.   LONG  lRetId;                       // Id by Value
  284.   pIdText->GetText(szBuf, 255);       // Get the Text for the Control ID
  285.   lRetId = StrToId(szBuf);            // Convert the define to its number
  286.   if (LOWORD(lRetId) == 0)            // if the number is blank
  287.     {
  288.     pIdText->SetSelection(0, 32767);  // Select a default Number
  289.     SetFocus(pIdText->HWindow);       // Set the focus to the Control
  290.     Msg.Result = (LONG)TRUE;          // return(TRUE);
  291.     return;                           // Return to Dialog for more input
  292.     }
  293.   Style->wId = HIWORD(lRetId);             // not blank - so set the wId
  294.   pTitle->GetText(szBuf, CTLTITLE);        // Get the Title from control
  295.   lstrcat(szBuf,"~");                      // Add delimiter
  296.   pRed->GetText(szBuffer, 4);              // Get Red Mix
  297.   lstrcat(szBuf,szBuffer);                 // Append to string
  298.   lstrcat(szBuf,"~");                      // Add delimiter
  299.   pGreen->GetText(szBuffer, 4);            // Get Green Mix
  300.   lstrcat(szBuf,szBuffer);                 // Append to string
  301.   lstrcat(szBuf,"~");                      // Add delimiter
  302.   pBlue->GetText(szBuffer, 4);             // Get Blue Mix
  303.   lstrcat(szBuf,szBuffer);                 // Append to string
  304.   lstrcpy(Style->szTitle, szBuf);          // Copy to structure
  305.   Style->dwStyle = WS_CHILD | WS_VISIBLE | wType ;
  306.   CloseWindow(TRUE);                       // Close Window and update
  307.   }                                        // Workshop Structure
  308. //-------------------------------------------------------------------
  309. #pragma argsused                  // displays dialog box in RW to allow
  310. BOOL FAR PASCAL _export ColRectStyle(HWND HWindow,         // user to
  311.                       HANDLE hCtlStyle,     // change
  312.                       LPFNSTRTOID StrToId,  // control
  313.                       LPFNIDTOSTR IdToStr)  // attributes
  314. {
  315.   PTColRectDlg    PDlg;
  316.   LPCTLSTYLE    PStyle  = (LPCTLSTYLE) GlobalLock(hCtlStyle);
  317.   BOOL          fRetval = FALSE;
  318.   if (PStyle)                // If Style locked OK
  319.     {                        // then Display dialog box
  320.       PDlg = new TColRectDlg(CtlColModule->GetParentObject(HWindow),
  321.              (LPSTR)MAKEINTRESOURCE(CTLBLUDLG),
  322.              (LPFNSTRTOID)StrToId,
  323.              (LPFNIDTOSTR)IdToStr,
  324.              (LPCTLSTYLE)PStyle,
  325.              CtlColModule);
  326.     fRetval = (IDOK == CtlColModule->ExecDialog(PDlg) );
  327.     GlobalUnlock(hCtlStyle);  // cleanup
  328.   }
  329.   return fRetval;             // return Flag to RW
  330. }
  331. //-------------------------------------------------------------------
  332. #pragma argsused                                   // Called by RW to
  333. WORD FAR PASCAL _export ColRectFlags(DWORD dwStyle, // translate style
  334.                       LPSTR szBuf,   // bits to text
  335.                       WORD wbufLen)  // for clarity
  336.   {
  337.   wsprintf(szBuf,(LPSTR)"0");  // No Style bits so use
  338.   return lstrlen(szBuf);       // the Character for zero
  339.   }
  340. //-------------------------------------------------------------------
  341. HANDLE FAR PASCAL _export ColRectInfo(void)  // Info function for RW
  342. {                                             // Default Control Settings
  343.   HANDLE hInfo = GlobalAlloc(GMEM_SHARE | GMEM_ZEROINIT, sizeof(RWCTLINFO));
  344.   if ( hInfo )
  345.     {
  346.     LPRWCTLINFO Info = (LPRWCTLINFO) GlobalLock(hInfo);
  347.     Info->wVersion = 0x0100;             // Version 1.00 of DLL
  348.     Info->wCtlTypes = 1;                 // Number of  controls in Function
  349.     lstrcpy(Info->szClass, szClassName);  // for instance of control
  350.     lstrcpy(Info->szTitle, "Rect~255~0~255"); // Caption
  351.                      // fill control structure
  352.     Info->Type[0].wType   = 0;           // Microsoft requires 0
  353.     Info->Type[0].wWidth  = 0x8000 | 31; // default width
  354.     Info->Type[0].wHeight = 0x8000 | 31; // default height
  355.     lstrcpy(Info->Type[0].szDescr, "Color Rectangle");//Must be different
  356.     Info->Type[0].dwStyle = WS_VISIBLE | WS_CHILD ;
  357.     Info->Type[0].hToolBit =            // Bitmap for Workshop palett
  358.       LoadBitmap(CtlColModule->hInstance, "CTLCOLBMP");
  359.     Info->Type[0].hDropCurs =           // Cursor for Workshop
  360.       LoadCursor(CtlColModule->hInstance, "CTLCOLCUR");
  361.     GlobalUnlock(hInfo);                // Unlock memory
  362.     }
  363.   return hInfo;                         // let RW have the memory
  364.   }
  365. //========================================================================
  366. #pragma argsused            // Used by RW to define controls
  367. extern "C" HANDLE FAR PASCAL _export ListClasses(LPSTR szAppName,
  368.                       WORD wVersion,
  369.                       LPFNLOADRES fnLoad,
  370.                       LPFNEDITRES fnEdit)
  371. {
  372.     HANDLE hClasses = GlobalAlloc(GMEM_SHARE | GMEM_ZEROINIT,
  373.     sizeof(int) + sizeof(RWCTLCLASS));
  374.     if ( hClasses )
  375.     {
  376.     LPCTLCLASSLIST Classes = (LPCTLCLASSLIST) GlobalLock(hClasses);
  377.     Classes->nClasses = 1;
  378.     Classes->Classes[0].fnRWInfo  = ColRectInfo;  // Three functions
  379.     Classes->Classes[0].fnRWStyle = ColRectStyle; // Workshop Calls
  380.     Classes->Classes[0].fnFlags   = (LPFNFLAGS)ColRectFlags;
  381.     lstrcpy(Classes->Classes[0].szClass, szClassName);
  382.     GlobalUnlock(hClasses);
  383.     }
  384.     fInWorkshop = TRUE;
  385.     return hClasses;
  386. }
  387. //------------------------------------------------------------------------
  388. WORD FAR PASCAL _export CTLCOLGetVersion(void)
  389.   {
  390.   return 0x0100;
  391.   }
  392. //------------------------------------------------------------------------
  393. int FAR PASCAL _export CTLCOLAbout(void)
  394.   {
  395.   return MessageBox(GetFocus(),"By Bob Bourbonnais",
  396.             "Custom Controls for Colored Rectangles",MB_OK);
  397.   }
  398. //------------------------------------------------------------------------
  399. #pragma argsused
  400. extern "C" int FAR PASCAL _export WEP(int nParameter)
  401. {
  402.   return (1);
  403. }
  404. //------------------------------------------------------------------------
  405. #pragma argsused
  406. extern "C" int FAR PASCAL LibMain(HINSTANCE hInstance, WORD wDataSeg,
  407.                  WORD cbHeapSize, LPSTR lpCmdLine)
  408. {
  409.   if (cbHeapSize > 0)
  410.     UnlockData(0);
  411.   CtlColModule = new TModule("ColRectMod", hInstance, lpCmdLine);
  412.   RegisterColRectClass(CtlColModule->hInstance);
  413.     fInWorkshop = FALSE;
  414.   return (1);
  415. }
  416. //=======================================================================//
  417.